<?php

// INSTALL SCRIPT. COMPLETES IN 4 STEPS
// Set the Error level manualy... I'm too lazy to fix notices
error_reporting(E_ALL ^ E_NOTICE);

require_once 'lib/general.lib.php';
require_once 'lib/class/config.class.php';
require_once 'lib/class/error.class.php'; 
require_once 'lib/class/vauth.class.php';
require_once 'lib/class/database_object.abstract.php';
require_once 'lib/class/preference.class.php'; 
require_once 'lib/class/access.class.php'; 
require_once 'lib/ui.lib.php';
require_once 'lib/log.lib.php'; 
require_once 'modules/horde/Browser.php';
require_once 'lib/install.php';
require_once 'lib/debug.lib.php';
require_once 'lib/gettext.php';

if ($_SERVER['HTTPS'] == 'on') { $http_type = "https://"; }
else { $http_type = "http://"; }

$prefix = dirname(__FILE__); 
Config::set('prefix',$prefix,'1'); 
// $configfile = $prefix . '\config\ampache.cfg.php';
$configfile = Config::get('prefix') . '\config.php';

set_error_handler('ampache_error_handler');

/* First things first we must be sure that they actually still need to install ampache 
*/
if (!install_check_status($configfile)) { 
	Error::display('general'); 
	//exit; 
}

// Define that we are doing an install so the includes will work
define('INSTALL','1'); 
define('INIT_LOADED','1');

/* Clean up incomming variables */
// Set defaults for the form, if they've already submited something 

$web_path = $_POST['web_path'] ? scrub_in($_POST['web_path']) : '/ampache'; 
$local_db = $_POST['local_db'] ? scrub_in($_POST['local_db']) : 'ampache'; 

$local_host = $_POST['local_host'] ? scrub_in($_POST['local_host']) : 'localhost'; 
$local_username = $_POST['local_username'] ? scrub_in($_POST['local_username']) : 'root'; 
$local_userpass = $_POST['local_userpass'] ; 

$db_username = $_POST['db_username'] ? scrub_in($_POST['db_username']) : 'AmpacheUser'; 
$db_userpass = $_POST['db_userpass'] ; 

$username = $_POST['username'] ? scrub_in($_POST['username']) : 'admin'; 
$password = $_POST['password'] ; 
$password2 = $_POST['password2'] ; 


if ($_SERVER['HTTPS'] == 'on') { $http_type = "https://"; }
else { $http_type = "http://"; }

define('WEB_PATH',$http_type . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/' . basename($_SERVER['PHP_SELF']));
define('WEB_ROOT',$http_type . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF'])); 

/* Catch the Current Action */
//echo 'Now: ' .$_REQUEST['action'];

switch ($_REQUEST['action']) { 

case 'create_db':
		
		// STEP #2 CREATE DB (STEP #1 is INIT at the end)
		
		/* Get the variables for the language */
		$htmllang = $_REQUEST['htmllang'];
		$charset  = $_REQUEST['charset'];

		// Set the lang in the conf array
		Config::set('lang', $htmllang,'1');
		Config::set('site_charset', $charset, '1');

		load_gettext();

		if (!install_insert_db($local_username,$local_userpass,$local_host,$local_db)) {
			require_once('templates/show_install.inc.php');
			break;
		}

		// install_create_admin_account();		
		// header ("Location: " . WEB_ROOT . "/login.php");

		// Now that it's inserted save the lang preference
		Preference::update('lang','-1',$htmllang);
		header ("Location: " . WEB_PATH . "?action=show_create_config&local_db=" . $local_db . "&local_host=" . $local_host . "&web_path=" . $web_path . "&htmllang=" . $htmllang . "&charset=" . $charset);
		
	break;
	
	case 'create_config':

		// Test and make sure that the values they give us actually work
		
		if (!check_database($local_host,$db_username,$db_userpass)) { 
			Error::add('config',_('Error: Unable to make Database Connection.<br>') . mysql_error());
		} 

		if (!Error::occurred()) { 
			$created_config = install_create_config($web_path,$db_username,$db_userpass,$local_host,$local_db);
		} 

		require_once('templates/show_install_config.inc.php');
		
	break;
	
	case 'show_create_config':
	
        /* Attempt to Guess the Web_path */
		$web_path = dirname($_SERVER['PHP_SELF']);
		$web_path = rtrim($web_path,"\/");

		/* Get the variables for the language */
		$htmllang = $_REQUEST['htmllang'];
		$charset  = $_REQUEST['charset'];

		// Set the lang in the conf array
		Config::set('lang', $htmllang,'1');

		// We need the charset for the different languages
		$charsets = array('de_DE' => 'ISO-8859-15',
				  'en_US' => 'iso-8859-1',
				  'en_GB' => 'UTF-8',
				  'ja_JP' => 'UTF-8',
				  'es_ES' => 'iso-8859-1',
				  'fr_FR' => 'iso-8859-1',
				  'el_GR' => 'el_GR.utf-8',
				  'it_IT' => 'UTF-8',
				  'nl_NL' => 'ISO-8859-15',
				  'tr_TR' => 'iso-8859-9',
				  'zh_CN' => 'GBK');
		$charset = $charsets[$_REQUEST['htmllang']];
		
		// Set the site_charset in the conf array
		Config::set('site_charset',$charsets[$_REQUEST['htmllang']],'1');
		
		/* load_gettext mojo */
		load_gettext();
		
		header ("Content-Type: text/html; charset=" . Config::get('site_charset'));
		require_once('templates/show_install_config.inc.php');

	break;
	
	case 'create_account':

		$results = parse_ini_file($configfile);
		Config::set_by_array($results,'1');

		/* Get the variables for the language */
		$htmllang = $_REQUEST['htmllang'];
		$charset  = $_REQUEST['charset'];

		// Set the lang in the conf array
		Config::set('lang', $htmllang,'1');
		Config::set('site_charset', $charset, '1');
		load_gettext();

		$username = scrub_in($_REQUEST['username']); 
		$password = scrub_in($_REQUEST['password']); 
		$password2 = scrub_in($_REQUEST['password2']); 

		if (!install_create_account($username,$password,$password2)) { 
			require_once(Config::get('prefix') . '/templates/show_install_account.inc.php');
			break;
		}

		header ("Location: " . WEB_ROOT . "/login.php");
		
	break;	
	
	
	case 'show_create_account':
	
		if(is_readable($configfile)) {
			$results = parse_ini_file($configfile);
		}
		else {
			Error::add('general',_('Error: Parsing config file impossible: ' . $configfile)); 
		}

		//print_r($results);
		//echo '<br>Config file = ' . $configfile;
		//echo '<br>Config result = ' . $results;
		
		/* Get the variables for the language */
		$htmllang = $_REQUEST['htmllang'];
		$charset  = $_REQUEST['charset'];

		// Set the lang in the conf array
		Config::set('lang', $htmllang,'1');
		Config::set('site_charset', $charset, '1');
		
		load_gettext();

		/* Make sure we've got a valid config file */
		if (!check_config_values($results)) { 
			Error::add('general',_('Error: Config file not found or Unreadable at ' . $configfile)); 
			require_once(Config::get('prefix') . '/templates/show_install_config.inc.php'); 
			break;
		}

		/* Get the variables for the language */
		$htmllang = $_REQUEST['htmllang'];
		$charset  = $_REQUEST['charset'];
		
		// Set the lang in the conf array
		Config::set('lang',$htmllang,'1');

		// We need the charset for the different languages
		$charsets = array('de_DE' => 'ISO-8859-15',
				  'en_US' => 'iso-8859-1',
				  'ja_JP' => 'UTF-8',
				  'en_GB' => 'UTF-8',
				  'es_ES' => 'iso-8859-1',
				  'fr_FR' => 'iso-8859-1',
				  'it_IT' => 'UTF-8',
				  'nl_NL' => 'ISO-8859-15',
				  'tr_TR' => 'iso-8859-9',
				  'zh_CN' => 'GBK');
		$charset = $charsets[$_REQUEST['htmllang']];
		
		// Set the site_charset in the conf array
		Config::set('site_charset',$charsets[$_REQUEST['htmllang']],'1');
		
		/* load_gettext mojo */
		load_gettext();
		header ("Content-Type: text/html; charset=" . Config::get('site_charset'));
		
		require_once Config::get('prefix') . '/templates/show_install_account.inc.php';
		
	break;

        case 'init':
		
		/* STEP 1: First step of installation */
		
		// Get language
		$htmllang = $_POST['htmllang'];

		// Set the lang in the conf array
		Config::set('lang',$htmllang,'1');

		// We need the charset for the different languages
		$charsets = array('de_DE' => 'ISO-8859-15',
				  'en_US' => 'iso-8859-1',
				  'cs_CZ' => 'UTF-8',
				  'ja_JP' => 'UTF-8',
				  'en_GB' => 'UTF-8',
				  'es_ES' => 'iso-8859-1',
				  'fr_FR' => 'iso-8859-1',
				  'it_IT' => 'UTF-8',
				  'nl_NL' => 'ISO-8859-15',
				  'tr_TR' => 'iso-8859-9',
				  'zh_CN' => 'GBK');

		$charset = $charsets[$_POST['htmllang']];

		// Set the site_charset in the conf array
        Config::set('site_charset',$charsets[$_POST['htmllang']],'1');
			
		// Now we make voodoo with the Load gettext mojo
		load_gettext();

		// Page ready  :)
		header ("Content-Type: text/html; charset=$charset");
		require_once('templates/show_install.inc.php');
		
	break;

	default:
		
		if ($_ENV['LANG']) {
			$lang = $_ENV['LANG'];
		} else {
			$lang = "en_US";
		}
		if(strpos($lang, ".")) {
			$langtmp = split("\.", $lang);
			$htmllang = $langtmp[0];
			$charset = $langtmp[1];
		} else {
			$htmllang = $lang;
			$charset = "UTF-8";
		}
		Config::set('lang',$htmllang,'1');
		Config::set('site_charset', $charset, '1');
		load_gettext();

		/* Show the language options first */
		require_once 'templates/show_install_lang.inc.php';
		
		
	break;
	
} // end action switch



?>
